#include "Common.h"
#include "FinPortfolio.h"
#include "FinScenario.h"

double **covarianceArray;
int currentScenario;
int finishedScenarios[6];
extern FinPortfolio scenarioPortfolio;
extern std::vector<InvestmentOption> investmentOptionVector;

FinScenario::FinScenario(QObject *parent) :
    QObject(parent)
{
    covarianceArray = NULL;
    this->scenarioStarted = false;
}

FinScenario::~FinScenario()
{
    if (covarianceArray != NULL)
    {
        free(covarianceArray);
    }
}

void FinScenario::initialize()
{
    int assets[MAX_YEARS];
    for(int i = 0; i <= MAX_YEARS; i++)
        assets[i] = 1000;

    scenarioPortfolio.setScenarioLiquidAssets(assets);

    //Initialize covariance array
    double *p;
    covarianceArray = (double**) malloc(sizeof(p) * investmentOptionVector.size());
    for(unsigned int i = 0; i < investmentOptionVector.size(); i++)
        covarianceArray[i] = (double *) calloc(investmentOptionVector.size(), sizeof(double));

    for(unsigned int i = 0; i < investmentOptionVector.size(); i++)
    {
        for(unsigned int j = 0; j < investmentOptionVector.size(); j++)
        {
            if(i == j)
                covarianceArray[i][j] = 1;
            else
                covarianceArray[i][j] = 0;
        }
    }

    //Setup new scenario weights
    covarianceArray[1][4] = -0.3;
    covarianceArray[4][1] = -0.3;
    covarianceArray[2][5] = -0.4;
    covarianceArray[5][2] = -0.4;
    covarianceArray[2][6] = 0.4;
    covarianceArray[6][2] = 0.4;

    currentScenario = 0;
    finishedScenarios[0] = 0;
    finishedScenarios[1] = 1;
    finishedScenarios[2] = 2;
    finishedScenarios[3] = 0;
    finishedScenarios[4] = 1;
    finishedScenarios[5] = 2;
}

bool FinScenario::isFinished()
{
    return currentScenario >= (int)(sizeof(finishedScenarios)/sizeof(*finishedScenarios));
}

bool FinScenario::isScenarioStarted()
{
    return scenarioStarted;
}

bool FinScenario::isFirstScenario()
{
    return currentScenario == 0;
}

int FinScenario::getCurrentScenario()
{
    return finishedScenarios[currentScenario];
}

void FinScenario::startScenario()
{
    scenarioPortfolio.removeAllInvestments();

    /*
     Random starts
     */
    /*if(first)
    {
        if(currentScenario == 0)
        {
            finishedScenarios[currentScenario] = rand() % 3;
        }
        else if(currentScenario == 1)
        {
            int i;
            do
            {
              i = rand() % 3;
              finishedScenarios[currentScenario] = i;
            } while( i == finishedScenarios[0]);

        }
        else
            finishedScenarios[currentScenario] = 3 - (finishedScenarios[0] + finishedScenarios[1]);
    }*/

    switch(finishedScenarios[currentScenario])
    {
        case 0:
            covarianceArray[3][2] = 0.0;
            covarianceArray[2][3] = 0.0;
            break;
        case 1:
            covarianceArray[3][2] = -0.8;
            covarianceArray[2][3] = -0.8;
            break;
        case 2:
            covarianceArray[3][2] = 0.8;
            covarianceArray[2][3] = 0.8;
            break;
    }

    scenarioStarted = true;
}

void FinScenario::stopScenario()
{
    scenarioStarted = false;
    currentScenario++;
}
